package com.dtranx.tools.corpora.ability.search;

import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import com.dtranx.tools.corpora.ability.vo.MatchingVo;
import com.dtranx.tools.corpora.abilityapi.enums.SearchMode;

import java.util.List;

/**
 * @ClassName ScoreModeSearch
 * @Description TODO
 * @Date 2024/3/12 16:50
 * @Author ly
 **/
public class ScoreModeSearch extends AbstractModeSearch{

    @Override
    public void buildQueryBuilder0(BoolQuery.Builder queryBuilder, MatchingVo matchingVo) {
        List<Query> queries = generateQueries(matchingVo, matchingVo.getValue());
        queryBuilder.filter(generateQueries0(matchingVo, matchingVo.getValue()));
        queryBuilder.must(mustCase->{
            mustCase.bool(builder -> {
//                builder.should(queries);
                builder.must(queries);
                return builder;
            });
            return mustCase;
        });
    }

//    @Override
//    protected List<Query> generateQueries0(MatchingVo matchingVo, String value) {
//        List<Query> queries = Lists.newArrayList();
//        queries.add(Query.of(queryBuilder -> {
//            //keyword全词匹配
//            queryBuilder.match(matchBuilder -> {
//                matchBuilder.field(matchingVo.isReverse() ? SearchType.KEYWORD_TYPE.getTranslationKey() : SearchType.KEYWORD_TYPE.getOriginalKey());
//                matchBuilder.boost(EsConstants.MAX_SCORE);
//                matchBuilder.query(value);
//                return matchBuilder;
//            });
//            return queryBuilder;
//        }));
//        //判断原文是否是小语种，决定使用什么分词器
//        SearchType searchType = matchingVo.isMinor() ? SearchType.TEXT_ANALYZER_STANDARD : SearchType.TEXT_ANALYZER_IK;
//        queries.add(Query.of(queryBuilder -> {
//            //包含匹配
//            queryBuilder.match(matchBuilder -> {
//                matchBuilder.field(matchingVo.isReverse() ? searchType.getTranslationKey() : searchType.getOriginalKey());
//                matchBuilder.boost(EsConstants.MID_SCORE);
//                matchBuilder.query(matchingVo.getValue());
//                return matchBuilder;
//            });
//            return queryBuilder;
//        }));
//        queries.add(Query.of(queryBuilder -> {
//            //模糊匹配
//            queryBuilder.match(matchBuilder -> {
//                matchBuilder.field(matchingVo.isReverse() ? searchType.getTranslationKey() : searchType.getOriginalKey());
//                matchBuilder.boost(EsConstants.MIN_SCORE);
//                matchBuilder.minimumShouldMatch(matchingVo.getFuzzy());
//                matchBuilder.query(matchingVo.getValue());
//                return matchBuilder;
//            });
//            return queryBuilder;
//        }));
//        queries.addAll(super.generateQueries0(matchingVo, value));
//        return queries;
//    }

    @Override
    public SearchMode getMode() {
        return SearchMode.SCORE;
    }
}
